package com.awaken.content.service.impl;

import com.awaken.content.mapper.AddressMapper;
import com.awaken.content.models.dto.AddressDTO;
import com.awaken.content.models.entity.Address;
import com.awaken.content.models.request.AddressRequest;
import com.awaken.content.service.AddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    
    @Override
    public AddressDTO createAddress(AddressRequest request) {
        Address address = new Address();
        BeanUtils.copyProperties(request, address);
        address.setStatus(1);
        address.setCreateTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        
        // 如果设置为默认地址，先取消其他默认地址
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", request.getUserId())
                    .set("is_default", false);
            update(updateWrapper);
        }
        
        save(address);
        
        AddressDTO dto = new AddressDTO();
        BeanUtils.copyProperties(address, dto);
        return dto;
    }
    
    @Override
    public AddressDTO getAddressById(Long id) {
        Address address = getById(id);
        if (address == null) {
            return null;
        }
        
        AddressDTO dto = new AddressDTO();
        BeanUtils.copyProperties(address, dto);
        return dto;
    }
    
    @Override
    public List<AddressDTO> getAddressesByUserId(Long userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("status", 1)
                .orderByDesc("is_default")
                .orderByDesc("create_time");
        List<Address> addresses = list(queryWrapper);
        
        return addresses.stream().map(address -> {
            AddressDTO dto = new AddressDTO();
            BeanUtils.copyProperties(address, dto);
            return dto;
        }).collect(Collectors.toList());
    }
    
    @Override
    public AddressDTO updateAddress(Long id, AddressRequest request) {
        Address address = getById(id);
        if (address == null) {
            return null;
        }
        
        BeanUtils.copyProperties(request, address);
        address.setUpdateTime(LocalDateTime.now());
        
        // 如果设置为默认地址，先取消其他默认地址
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", request.getUserId())
                    .ne("id", id)
                    .set("is_default", false);
            update(updateWrapper);
        }
        
        updateById(address);
        
        AddressDTO dto = new AddressDTO();
        BeanUtils.copyProperties(address, dto);
        return dto;
    }
    
    @Override
    public void deleteAddress(Long id) {
        Address address = getById(id);
        if (address != null) {
            address.setStatus(0);
            address.setUpdateTime(LocalDateTime.now());
            updateById(address);
        }
    }
    
    @Override
    public AddressDTO setDefaultAddress(Long id, Long userId) {
        // 先取消所有默认地址
        UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId)
                .set("is_default", false);
        update(updateWrapper);
        
        // 设置新的默认地址
        Address address = getById(id);
        if (address != null) {
            address.setIsDefault(true);
            address.setUpdateTime(LocalDateTime.now());
            updateById(address);
            
            AddressDTO dto = new AddressDTO();
            BeanUtils.copyProperties(address, dto);
            return dto;
        }
        return null;
    }
} 